

////弹框
//function confirmHtml(text, LeaveVoteNum, flag) {
//    var html = '<div class="weui-dialog weui-dialog--visible" id="confirm">'
//           + '<div class="weui-dialog__hd">'
//           + '   <strong class="weui-dialog__title">'
//           + '       <img src="/images/sf.png">'
//           + text
//           + '  </strong>'
//           + ' </div><div class="weui-dialog__bd"></div>'
//           + ' <div class="weui-dialog__ft">'
//           + '     <a onclick="CancelConfirm(this)" LeaveVoteNum=' + LeaveVoteNum + ' class="weui-dialog__btn primary">确定</a>'
//           + ' </div>'
//           + ' </div>';
//    $('body').append(html);
//    $(".weui-mask,.weui-dialog").show();
//}
////弹框取消
//function CancelConfirm(obj) {
//    var LeaveVoteNum = $(obj).attr("LeaveVoteNum");
//    if (LeaveVoteNum == 0) {
//        window.location.href = "http://m.gxanr.com";
//    }
//    $(".weui-mask--visible").hide();
//    $("#confirm").remove();
//}


//kkaper分页
function getParameter(name) {

    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");

    var r = window.location.search.substr(1).match(reg);

    if (r != null) return unescape(r[2]); return null;
}

//init
function getPage(pno, totalPage, totalRecords) {

    var pageNo = getParameter('pno');
    if (!pageNo) {
        pageNo = 1;
    }
    //初始化分页页面
    kkpager.init({
        pagerid: 'kkpager',
        pno: pno,	//当前页	 
        mode: 'click', //设置为click模式
        //总页码
        total: totalPage,
        //总数据条数
        totalRecords: totalRecords,
        click: function (n) {
            switch (itemSelect) {
                case 1: queryTrade(n); break;
                case 2: querySellerTrade(n); break;
                case 3: queryDrawCash(n); break;
                case 4: queryRecharge(n); break;
                case 5: queryOderRefundRecharge(n); break;
                case 6: queryOderCreditRecharge(n); break;
                case 7: queryOderCreditHKRecharge(n); break;
                case 8: queryOderDueRecharge(n); break;
                case 9: queryBuyerRecharge(n); break;
                case 10: querySellerRecharge(n); break;
                default: queryTrade(n);
            }

            this.selectPage(n);
        },
        //getHref是在click模式下链接算法，一般不需要配置，默认代码如下
        getHref: function (n) {
            return '#';
        }
    });
    //生成分页
    kkpager.generPageHtml();
}

//分页的参数设置
function pageData(pno, json) {

    var totalpage;//总页数
    var pageRecord = 10;//每页记录
    var totalRecord;//记录总数
    var bal;//余数
    if (json == null) {
        totalpage = 1;
        totalRecord = 0;

    } else {
        totalpage = json.length / pageRecord;
        bal = json.length % pageRecord;
        totalRecord = json.length;

        if (json.length <= pageRecord) {

            totalpage = 1;
        } else if (bal != 0) {
            totalpage = totalpage + 1;
        }

    }

    if (pno == 0)
        pno = 1;

    getPage(pno, totalpage, totalRecord);
    var nowrecord;//当前分页的起始记录
    var nowMaxRecord;//当前页的最大记录

    if (pno == 1) {
        nowrecord = 0;
    } else {
        nowrecord = pageRecord * (pno - 1);
    }
    if (json == null) {
        nowMaxRecord = 0;
    } else if (nowrecord + pageRecord > json.length) {
        nowMaxRecord = json.length;//当前的剩余记录不足每页最大记录
    } else {
        nowMaxRecord = nowrecord + pageRecord;
    }
    var jsonText = { "nowMaxRecord": nowMaxRecord, "nowrecord": nowrecord };

    return jsonText;
}


// 错误提示框
function ErrorBox(msg, focusObj, parent) {
    $.dialog(
    {
        id: "ErrorBox",
        title: '提示信息',
        lock: true,
        min: false,
        max: false,
        content: msg,
        icon: 'error.gif',
        parent: parent,
        zIndex: 2000,
        ok: function () {
            $(focusObj).focus();
            return true;
        }
    });
}

//信息提示框
function AlertBox(msg) {
    $.dialog(
    {
        id: "AlertBox",
        title: '提示信息',
        lock: true,
        min: false,
        max: false,
        content: msg,
        icon: 'alert.gif',
        zIndex: 2000,
        ok: function () {
            return true;
        }
    });
}

//成功提示框
function SuccessBox(msg, isReload, callbackFun) {
    $.dialog(
    {
        id: "SuccessBox",
        title: '提示信息',
        lock: true,
        min: false,
        max: false,
        content: msg,
        icon: 'success.gif',
        zIndex: 2000,
        ok: function (here) {
            if (isReload) {
                window.location.reload();
            }
            return callbackFun != null ? callbackFun.call(this, here) : true;
        }
    });
}

//成功提示后重定向
function SuccessBoxRedirect(msg, url) {
    $.dialog(
    {
        id: "SuccessBox",
        title: '提示信息',
        lock: true,
        min: false,
        max: false,
        content: msg,
        icon: 'success.gif',
        ok: function () {
            window.location.href = url;
        },
        close: function () {
            window.location.href = url;
        }
    });
}

//confirm提示框
function ConfirmBox(msg, callbackFun) {
    $.dialog.confirm(msg, function (here) {
        return callbackFun.call(this, here);
    });
}

//自定义提示框
function CustomBox(title, msg, callbackFun, time, icon) {
    $.dialog({
        id: "customBox",
        time: time,
        title: title,
        lock: true,
        min: false,
        max: false,
        content: msg,
        icon: icon,
        ok: function (here) { return callbackFun.call(this, here); },
        cancel: true
    });
}

/*
* 信息提示框
* msg:提示内容
* type:提示类型(值分别有:loading,success,error,alert,confirm,tips)
* time:停留时间(默认值: 1)
* isLock:是否锁屏
* parent:父窗口
* 示例(loading调用)：
*       var loading = TipsBox("数据提交中，请耐心等待...", "loading", 60, true, parent); 
*       loading.close();//关闭loading
*
*/
function TipsBox(msg, type, time, isLock, parent) {
    var tm = time || 1;
    var icon = type ? type + ".gif" : "";
    var t;
    if (type == "loading") {
        t = $.dialog({
            id: "loadTest",
            title: false,
            lock: isLock,
            parent: parent,
            content: msg,
            icon: 'loading.gif',
            zIndex: 2000,
            ok: false,
            cancel: false
        });

        //var timer = setTimeout(function () {
        //    console.log(t.closed);
        //    if (t.closed == false) {
        //        t.close();
        //        $.dialog.tips("请求超时，请您稍后再试...", 1, "error.gif");
        //    } else {
        //        clearTimeout(timer);
        //    }
        //}, 6000);

    } else {
        t = $.dialog.tips(msg, tm, icon);
        if (isLock == 1 || isLock == true) {
            t.lock();
        }
    }
    return t;
}

//分页刷新
function loadListResult(url, fillId, obj) {
    if (fillId && fillId.length > 0) {
        var thisParent = $(obj).parent("#mini");

        if (thisParent.length > 0) {
            var prefixUrl = url.substring(url, url.lastIndexOf("=") + 1);
            var cPage = url.substring(url.lastIndexOf("=") + 1);
            var totalPage = parseInt(thisParent.children("em:eq(1)").text());

            if (cPage == 0 || cPage > totalPage) {
                return false;
            }
            thisParent.children("em:eq(0)").text(cPage);
            url = prefixUrl + cPage;
        }
        //处理IE8,9的缓存问题
        url +="&t=" + (new Date).valueOf();
        var loading = TipsBox("", "loading", 60, false);
        $.get(url, function (result) {
            if (loading != null) {
                loading.close();
            }
            $("#" + fillId).html(result);
        });
    } else {
        window.location.href = url;
    }
    return true;
}

//跳到某一页
function gotoPage(obj, url, loadingId) {
    var page = $(obj).parent().children('[name="pageNo"]').val();
    var totalPage = $("#totalPage").val();
    if (page != "") {
        if (parseInt(page) == 0) {
            page = 1;
        } else if (parseInt(page) > parseInt(totalPage)) {
            page = totalPage;
        }
    }
    if (url != undefined && url != "") {
        url = url.replace("{0}", parseInt(page));
    }
    if (page != "") {
        loadListResult(url, loadingId, obj);
    }
}

/*
* 全选操作
* curObj:当前操作对象
* chkTag:要选择的标签name属性值
*/
function chkAll(curObj, chkTag) {
    var isChecked = $(curObj).prop("checked");
    $("input[name='" + chkTag + "']").prop("checked", isChecked);
    $("input[name='chkAll']").prop("checked", isChecked);
}

function customDialog(msgInfo, icon, parent, callbackFun) {
    $.dialog({
        id: "customBox",
        title: '提示信息',
        lock: true,
        min: false,
        max: false,
        content: msgInfo,
        zIndex: 2999,
        icon: icon,
        parent: parent,
        ok: function (here) {
            $("#ldg_lockmask").css("z-index", 1975);
            if (callbackFun != null && typeof (eval(callbackFun)) == "function") {
                return callbackFun.call(this, here);
            }
            else {
                return true;
            }

        }
    });
}

function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();


    var strsec = getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec * 1);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//读取cookies
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))

        return (arr[2]);
    else
        return null;
}

//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

jQuery.SetBG = function () {
    var tip = "<div id='myBg_for_document' style='position: fixed; _position:absolute; top: 0px; left: 0px; width: 2423px; height: 4140px; background-color: rgb(0, 0, 0); opacity: 0.3;filter:alpha(opacity=30); z-index: 10000;'></div>";
    var myBg_for_document = $("#myBg_for_document");
    if (myBg_for_document = null || myBg_for_document.length == 0) {
        $(document.body).append(tip);
    } else {
        myBg_for_document.show();
    }
};
//关闭遮罩层
jQuery.CloseBG = function () {
    var myBg = $("#myBg_for_document");
    if (myBg && myBg.length > 0) {
        $("#myBg_for_document").remove();
    }
}
//固定设置屏幕居中
jQuery.SetMiddle = function (selector) {
    var layer = $(selector);
    var layer_width = layer.width();
    var layer_height = layer.height();
    layer.css({ "top": (document.body.clientHeight / 2 - layer_height / 2), "left": document.body.clientWidth / 2 - layer_width / 2 - 10 });

};


//以下是验证价格的
function chkPrice(obj) {
    if (obj.value == "") { return; }
    obj.value = obj.value.replace(/[^\d.]/g, "");
    //必须保证第一位为数字而不是. 
    obj.value = obj.value.replace(/^\./g, "");
    //保证只有出现一个.而没有多个. 
    obj.value = obj.value.replace(/\.{2,}/g, ".");
    //保证.只出现一次，而不能出现两次以上 
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}
function chkLast(obj) {
    if (obj.value == "") { return; }
    // 如果出现非法字符就截取掉
    if (obj.value.substr((obj.value.length - 1), 1) == '.') {
        obj.value = obj.value.substr(0, (obj.value.length - 1));
    }
    //保证只能获取两位小数
    obj.value = parseFloat(obj.value).toFixed(2);
}

function chkLast2(obj) {
    if (obj.value == "") { return; }
    // 如果出现非法字符就截取掉
    if (obj.value.substr((obj.value.length - 1), 1) == '.') {
        obj.value = obj.value.substr(0, (obj.value.length - 1));
    }
    //保证只能获取一位小数
    obj.value = parseFloat(obj.value).toFixed(1);
}


/**
当图片出错时调用
imgObj 当前img对象
isQuuare 是否正方形显示，传true or false
maxErrorNum 在出错次数范围内仍找不到错误提示图显示缺省图片
**/
function showErrorImgDelay(imgObj) {//maxErrorNum为加载图片出错的次数
    var imgSrc = "../../Content/image/nopic.jpg";
    $(imgObj).attr("err", true);
    setTimeout(function () {
        $(imgObj).attr("src", imgSrc);
    }, 100);
}

/**
当图片出错时调用
**/
function showErrorImg(imgObj) {
    var imgSrc = "../../Content/image/nopic.jpg";
    $(imgObj).attr("err", true);
    setTimeout(function () {
        $(imgObj).attr("src", imgSrc);
    }, 100);
}

/**
验证填写的是否为数字
**/
function chkNum(obj) {
    obj = $(obj);
    if (obj.val() == "") { return; }
    obj.val(obj.val().replace(/[^\d.]/g, ""));
    //必须保证每一位为数字
    obj.val(obj.val().replace(/[^0-9]/ig, ""));
}

/**
解决IE8 placeholder 不兼容的问题
**/
function compatiblePlaceholder() {

    ////判断浏览器是否支持placeholder属性
    //supportPlaceholder = 'placeholder' in document.createElement('input'),

    //placeholder = function (input) {

    //    var text = input.attr('placeholder'),
    //    defaultValue = input.defaultValue;

    //    if (!defaultValue) {

    //        input.val(text).addClass("phcolor");
    //    }

    //    input.focus(function () {

    //        //if (input.val() == text) {

    //        //    $(this).val("");
    //        //}
    //    });

    //    input.blur(function () {

    //        if (input.val() == "") {

    //            $(this).val(text).addClass("phcolor");
    //        }
    //    });

    //    //输入的字符不为灰色
    //    input.keydown(function () {

    //        $(this).removeClass("phcolor");
    //    });
    //};

    ////当浏览器不支持placeholder属性时，调用placeholder函数
    //if (!supportPlaceholder) {

    //    $('input').each(function () {

    //        text = $(this).attr("placeholder");

    //        if ($(this).attr("type") == "text" || $(this).attr("type") == "password") {

    //            placeholder($(this));
    //        }
    //    });
    //}
    var placeholderfriend = {
        focus: function (s) {
            s = $(s).hide().prev().show().focus();
            var idValue = s.attr("id");
            if (idValue) {
                s.attr("id", idValue.replace("placeholderfriend", ""));
            }
            var clsValue = s.attr("class");
            if (clsValue) {
                s.attr("class", clsValue.replace("placeholderfriend", ""));
            }
        }
    }
    //判断是否支持placeholder
    function isPlaceholer() {
        var input = document.createElement('input');
        return "placeholder" in input;
    }
    //不支持的代码
    if (!isPlaceholer()) {
        $(function () {
            var form = $(this);
            //遍历所有文本框，添加placeholder模拟事件
            var elements = form.find("input[type='text'][placeholder]");
            elements.each(function () {
                var s = $(this);
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (pValue) {
                    if (sValue == '') {
                        s.val(pValue).addClass("phcolor");
                    }
                }
            });
            elements.focus(function () {
                var s = $(this);
                s.removeClass("phcolor");
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (sValue && pValue) {
                    if (sValue == pValue) {
                        s.val('');
                        s.addClass("phcolor");
                    }
                }
            });
            elements.blur(function () {
                var s = $(this);
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (!sValue) {
                    s.val(pValue);
                    s.addClass("phcolor");
                }
            });
            elements.keydown(function () {
                var s = $(this);
                s.removeClass("phcolor");
             });
            //遍历所有密码框，添加placeholder模拟事件
            var elementsPass = form.find("input[type='password'][placeholder]");
            elementsPass.each(function (i) {
                var s = $(this);
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (pValue) {
                    if (sValue == '') {
                        s.addClass("phcolor");
                        //DOM不支持type的修改，需要复制密码框属性，生成新的DOM
                        var html = this.outerHTML || "";
                        html = html.replace(/\s*type=(['"])?password\1/gi, " type=text placeholderfriend")
                          .replace(/\s*(?:value|on[a-z]+|name)(=(['"])?\S*\1)?/gi, " ")
                          .replace(/\s*placeholderfriend/, " placeholderfriend value='" + pValue
                          + "' " + "onfocus='placeholderfriendfocus(this);' ");
                        var idValue = s.attr("id");
                        if (idValue) {
                            s.attr("id", idValue + "placeholderfriend");
                        }
                        var clsValue = s.attr("class");
                        if (clsValue) {
                            s.attr("class", clsValue + "placeholderfriend");
                        }
                        s.hide();
                        s.after(html);
                    }
                }
            });
            elementsPass.focus(function () {
                var s = $(this);
                s.removeClass("phcolor");
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (sValue && pValue) {
                    if (sValue == pValue) {
                        s.val('');
                        s.addClass("phcolor");
                    }
                }
            });
            elementsPass.blur(function () {
                var s = $(this);
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (sValue == '') {
                    var idValue = s.attr("id");
                    if (idValue) {
                        s.attr("id", idValue + "placeholderfriend");
                    }
                    var clsValue = s.attr("class");
                    if (clsValue) {
                        s.attr("class", clsValue + "placeholderfriend");
                    }
                    s.hide().next().val(pValue).addClass("phcolor").show();
                }
            });
        });
    }
    window.placeholderfriendfocus = placeholderfriend.focus;
}

/**
日期格式化
**/
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "H+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

/**
当图片出错时调用
imgObj 当前img对象
isQuuare 是否正方形显示，传true or false
maxErrorNum 在出错次数范围内仍找不到错误提示图显示缺省图片
**/
function showErrorImgDelay(imgObj, isSquare, maxErrorNum) {//maxErrorNum为加载图片出错的次数
    if (maxErrorNum > 0) {
        var imgSrc;
        if (isSquare) {
            //imgSrc = "../../Content/image/defaut.png";
            imgSrc = "../../Content/image/defaultPic.jpg";
        } else {
            //imgSrc = "../../Content/image/df.png";
            imgSrc = "../../Content/image/defaultPic.jpg";
        }
        $(imgObj).attr("err", true);
        imgObj.onerror = function () {
            showErrorImgDelay(imgObj, imgSrc, maxErrorNum - 1);
        };
        setTimeout(function () {
            imgObj.src = imgSrc;
        }, 500);
    } else {
        imgObj.onerror = null;
        //imgObj.src = "image/default.jpg";
        imgObj.src = "../../Content/image/defaultPic.jpg";
    }
}

//显示找不到图片时的用户默认头像
function showErrorImgDelayForUser(imgObj, maxErrorNum) {//maxErrorNum为加载图片出错的次数
    if (maxErrorNum > 0) {
        var imgSrc = "../../Content/image/portrait_default.png";
        imgObj.onerror = function () {
            showErrorImgDelay(imgObj, imgSrc, maxErrorNum - 1);
        };
        setTimeout(function () {
            imgObj.src = imgSrc;
        }, 500);
    } else {
        imgObj.onerror = null;
        imgObj.src = "image/defaut.jpg";
    }
}
